
//911.在线选举
class TopVotedCandidate {
    //使用map来记录每个人获得票数的时间
    unordered_map<int,vector<int>> all; 

public:
    TopVotedCandidate(vector<int>& persons, vector<int>& times) {
        int n=times.size();
        for(int i=0;i<n;i++)
            all[persons[i]].push_back(times[i]);
    }
    
    int q(int t) {
        int ret=0,tmp=0,time=0;
        //对每个人进行查找
        for(auto& [i,each]:all)
        {
            int count=upper_bound(each.begin(),each.end(),t)-each.begin();
            if(count==0) continue;
            if(tmp<count||(tmp==count&&time<each[count-1])) tmp=count,ret=i,time=each[count-1];
        }        
        return ret;
    }
};

/**
 * Your TopVotedCandidate object will be instantiated and called as such:
 * TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
 * int param_1 = obj->q(t);
 */